<!-- 选择科室页面 -->
<template>
  <div class="xzks-container">
    <!-- 医院卡片 -->
    <div class="hospital-card">
      <img class="hospital-logo" :src="params.logo" alt="logo" />
      <div class="hospital-info">
        <div class="hospital-title">{{ params.title || '医院名称' }}</div>
        <div class="hospital-tags">
          <span class="tag">{{ params.grade || '医院等级' }}</span>
          <span class="tag tag-type">{{ params.category || '医院类型' }}</span>
        </div>
        <div class="hospital-address">地址：{{ params.address || '医院地址' }}</div>
      </div>
    </div>

    <!-- 科室列表标题 -->
    <div class="section-title" v-if="!selectedDept">
      <span class="section-bar"></span>
      <span class="section-title-text">科室列表</span>
    </div>

    <!-- 科室卡片列表 -->
    <div v-if="!selectedDept" class="dept-list">
      <div class="dept-card" v-for="(item, idx) in depts" :key="idx" @click="selectDept(item)">
        <div class="dept-name">{{ item.title }}</div>
        <div class="dept-underline"></div>
      </div>
    </div>

    <!-- 医生列表 -->
    <div v-else>
      <div class="doctor-section-title">
        <span class="doctor-section-bar"></span>
        <span class="doctor-section-title-text">医生列表</span>
      </div>
      <div class="doctor-list">
        <div class="doctor-card" v-for="doc in doctors || []" :key="doc.name">
          <img class="doctor-avatar" :src="doc.avatar" alt="头像" />
          <div class="doctor-info">
            <div class="doctor-row">
              <span class="doctor-name">{{ doc.name }}</span>
              <span class="doctor-title">{{ doc.grade }}</span>
              <span class="doctor-dept">{{ doc.depa }}</span>
            </div>
            <div class="doctor-hospital">{{ doc.h_title }}</div>
            <div class="doctor-goodat">
              擅长：<span class="goodat-keyword">{{ doc.good_at }}</span>
            </div>
          </div>
          <button class="doctor-action" @click="goToYyxx(doc)">预约</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import type { YyghDoctorDataItemResult } from '@/types/home'
import { getYyghDeptData, getYyghDoctorByDept } from '@/https/HomeHTTP'

const depts = ref([])
const getDept = async () => {
  const res = await getYyghDeptData(params.value.hid)
  depts.value = res.data.result
}
const doctors = ref([])
const getDoctors = async (dept) => {
  const res = await getYyghDoctorByDept(dept.did, dept.hid)
  doctors.value = res.data
}

const selectedDept = ref('')
const params = ref({})

onLoad(async (query) => {
  params.value = query
  console.log(params.value)
  await Promise.all([getDept()])
})

function selectDept(dept) {
  selectedDept.value = dept
  getDoctors(dept)
}
const goToYyxx = (item) => {
  console.log(item)
  uni.navigateTo({
    url: `/pages/component/yyxx/yyxx?did=${encodeURIComponent(item.did)}&hid=${encodeURIComponent(
      item.hid,
    )}&name=${encodeURIComponent(item.name)}&grade=${encodeURIComponent(
      item.grade,
    )}&avatar=${encodeURIComponent(item.avatar)}&good_at=${encodeURIComponent(
      item.good_at,
    )}&depa=${encodeURIComponent(item.depa)}&h_title=${encodeURIComponent(
      item.h_title,
    )}&h_addr=${encodeURIComponent(item.h_addr)}`,
  })
}
</script>

<style lang="scss" scoped>
.xzks-container {
  background: #f5f5f5;
  min-height: 100vh;
  font-family: 'PingFang SC', 'Microsoft YaHei', Arial, sans-serif;
  padding-top: 0 !important;
}
page, body, html {
  background: #f5f5f5 !important;
}
.hospital-card {
  display: flex;
  align-items: center;
  background: #fff;
  border-radius: 10px;
  box-shadow: 0 2px 8px 0 #e6f7e6;
  margin: 14px 12px 18px 12px;
  padding: 14px 18px 14px 18px;
}
.hospital-logo {
  width: 56px;
  height: 56px;
  border-radius: 6px;
  object-fit: cover;
  background: #eaeaea;
  margin-right: 16px;
  border: 1px solid #e0e0e0;
}
.hospital-info {
  flex: 1;
  min-width: 0;
}
.hospital-title {
  font-size: 22px;
  font-weight: bold;
  color: #222;
  margin-bottom: 8px;
  line-height: 1.2;
  text-align: left;
}
.hospital-tags {
  margin-bottom: 8px;
  display: flex;
  align-items: center;
}
.tag {
  display: inline-block;
  background: #3ec99e;
  color: #fff;
  font-size: 15px;
  font-weight: bold;
  border-radius: 5px;
  padding: 2px 14px 2px 14px;
  margin-right: 8px;
  line-height: 1.1;
}
.tag-type {
  background: #e6f7e6;
  color: #3ec99e;
  font-weight: bold;
  border: 1px solid #3ec99e;
}
.hospital-address {
  display: flex;
  align-items: center;
  color: #222;
  font-size: 16px;
  margin-top: 2px;
}
.section-title {
  display: flex;
  align-items: center;
  margin: 0 0 18px 24px;
}
.section-bar {
  width: 6px;
  height: 24px;
  background: #00d200;
  border-radius: 3px;
  margin-right: 10px;
  display: inline-block;
}
.section-title-text {
  font-size: 22px;
  font-weight: bold;
  color: #555;
}
.dept-list {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  justify-content: center;
  margin: 0;
  padding: 0;
}
.dept-card {
  width: 44vw;
  max-width: 160px;
  height: 80px;
  background: #3ec99e;
  border-radius: 10px;
  box-shadow: 0 2px 8px 0 #e6f7e6;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: flex-start;
  padding: 12px 0 0 12px;
  margin-bottom: 0;
  cursor: pointer;
  transition: box-shadow 0.2s;
  &:hover {
    box-shadow: 0 4px 16px 0 #b6f5b6;
  }
}
.dept-name {
  font-size: 18px;
  font-weight: bold;
  color: #fff;
  margin-bottom: 6px;
  text-align: left;
}
.dept-underline {
  width: 45px;
  height: 4px;
  background: #fff;
  border-radius: 2px;
}
// 医生列表部分
.doctor-section-title {
  display: flex;
  align-items: center;
  margin: 0 0 18px 24px;
}
.doctor-section-bar {
  width: 6px;
  height: 24px;
  background: #00d200;
  border-radius: 3px;
  margin-right: 10px;
  display: inline-block;
}
.doctor-section-title-text {
  font-size: 22px;
  font-weight: bold;
  color: #555;
}
.doctor-list {
  margin: 0 0 0 0;
  padding: 0 18px 0 18px;
}
.doctor-card {
  display: flex;
  align-items: center;
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 2px 8px 0 #e6f7e6;
  margin-bottom: 18px;
  padding: 18px 18px 18px 18px;
  position: relative;
}
.doctor-avatar {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  object-fit: cover;
  background: #eaeaea;
  margin-right: 18px;
  border: 1px solid #e0e0e0;
}
.doctor-info {
  flex: 1;
  min-width: 0;
}
.doctor-row {
  display: flex;
  align-items: center;
  margin-bottom: 6px;
}
.doctor-name {
  font-size: 22px;
  font-weight: bold;
  color: #222;
  margin-right: 8px;
}
.doctor-title {
  background: #2583e7;
  color: #fff;
  font-size: 15px;
  font-weight: bold;
  border-radius: 4px;
  padding: 2px 10px;
  margin-right: 6px;
}
.doctor-dept {
  background: #e6f0fa;
  color: #2583e7;
  font-size: 15px;
  font-weight: bold;
  border-radius: 4px;
  padding: 2px 10px;
}
.doctor-hospital {
  font-size: 16px;
  color: #444;
  margin-bottom: 4px;
}
.doctor-goodat {
  font-size: 16px;
  color: #444;
}
.goodat-keyword {
  color: #ff6600;
  font-weight: bold;
}
.doctor-action {
  background: #3ec99e;
  color: #fff;
  border: none;
  border-radius: 7px;
  font-size: 15px;
  font-weight: bold;
  padding: 3px 14px;
  position: absolute;
  right: 20px;
  top: 36px;
  cursor: pointer;
  box-shadow: 0 2px 8px 0 #b6f5b6;
  transition: background 0.2s;
  &:hover {
    background: #00b97b;
  }
}
</style>